package com.niannian.activity;

import java.util.Date;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.niannian.BaseActivity;
import com.niannian.R;
import com.niannian.util.Common;
import com.niannian.view.HandyTextView;

/**
 * 提醒弹框
 * 
 * @author shenglin
 * 
 */
public class DateTipPopping extends BaseActivity {
	public static final int TIP_HIDE = 1;
	HandyTextView tv_message_title, tv_message_content;
	Button btn_confim, btn_cancel;
	TextView tv_tip_date, tv_tip_date_num_tip1, tv_tip_date_num, tv_tip_date_num_tip2;
	String title = "";
	String content = "";
	int sender = 0;
	String item_time = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.date_tip_toast);
		addActToGroup("datetip", this);
		try {
			title = getIntent().getStringExtra("title");
			content = getIntent().getStringExtra("content");
			sender = getIntent().getIntExtra("id", 0);

			item_time = Common.getChangeTime(getIntent().getStringExtra("item_time"));
		} catch (Exception e) {
			title = "";
			content = "";
			item_time = "0";
		}

		initView();
		setLister();
		nHandler.sendEmptyMessageDelayed(TIP_HIDE, 60000);
	}

	private void initView() {

		tv_message_title = (HandyTextView) findViewById(R.id.tv_message_title);
		tv_message_content = (HandyTextView) findViewById(R.id.tv_message_content);
		btn_cancel = (Button) findViewById(R.id.btn_cancel);
		btn_confim = (Button) findViewById(R.id.btn_confim);

		tv_message_title.setText(title + "");
		tv_message_content.setText(content + "");

		// SharedPreferences sp = getSharedPreferences("userinfo",
		// Context.MODE_PRIVATE);
		// if (sp.getBoolean("isvibrate", true)) {
		// Vibrator vibrator = (Vibrator)
		// getSystemService(Context.VIBRATOR_SERVICE);
		// //long[] pattern = { 3000 };
		// //vibrator.vibrate(pattern,1);
		// vibrator.vibrate(3000);
		// }

		tv_tip_date = (TextView) findViewById(R.id.tv_tip_date);
		tv_tip_date_num_tip1 = (TextView) findViewById(R.id.tv_tip_date_num_tip1);
		tv_tip_date_num = (TextView) findViewById(R.id.tv_tip_date_num);
		tv_tip_date_num_tip2 = (TextView) findViewById(R.id.tv_tip_date_num_tip2);

		int time = (Integer) Integer.parseInt(item_time) - Integer.parseInt(Common.getChangeTime(Common.sgmdate3((int) ((new Date()).getTime() / 1000))));
		int timeday = (time / 86400);
		tv_tip_date_num_tip1.setVisibility(View.GONE);
		tv_tip_date_num_tip2.setVisibility(View.VISIBLE);

		tv_tip_date_num.setTextColor(getResources().getColor(R.color.col_f));
		if (timeday <= 0) {
			tv_tip_date_num.setText("今");
		} else if (timeday == 1) {
			tv_tip_date_num.setText("明");
		} else if (timeday == 2) {
			tv_tip_date_num.setText("后");
		} else {
			tv_tip_date_num.setText(timeday + "");
			tv_tip_date_num_tip1.setVisibility(View.VISIBLE);
			tv_tip_date_num.setTextColor(getResources().getColor(R.color.col_e));
		}

		String Specdate = Common.sgmdateMD(Integer.parseInt(item_time));
		tv_tip_date.setText(Specdate.split("/")[0] + "月" + Specdate.split("/")[1] + "日");

	}

	private void setLister() {
		btn_cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				destroyGroup("datetip");
				myfinish();

			}
		});
		btn_confim.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putInt("id", sender);
				openActivity(DateTipActivity.class, bundle);
				destroyGroup("datetip");
				myfinish();
			}
		});

	}

	Handler nHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {

			case TIP_HIDE:
				destroyGroup("datetip");
				myfinish();
				break;

			default:
				break;
			}
		};
	};

}
